package com.pattern.singleton;

/**
 * Lazy loading 懒汉式
 * 通过synchronized解决线程安全问题
 * 加上锁 效率会下降
 */
public class Mgr04 {

    private static Mgr04 INSTANCE;

    private Mgr04(){
    }

    public static synchronized Mgr04 getInstance(){
        if (INSTANCE == null) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new Mgr04();
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        Long start = System.currentTimeMillis();
        for(int i = 0; i < 100; i++){
            new Thread(() -> {
                System.out.println(Mgr04.getInstance().hashCode());
            }).start();
        }
        Long end = System.currentTimeMillis();
        System.out.println("mill: " + (end - start));
    }
}
